package top.resty.erp.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.resty.erp.domain.Type;
import top.resty.erp.service.TypeService;
import top.resty.erp.util.LayuiJson;

import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author : resty
 * @date : 2021年03月23 00:06
 */
@Controller
@RequestMapping("/type")
public class TypeContrlooer {
	
	@Autowired
	private TypeService typeService;
	
	@RequestMapping("/forVarietyId")
	@ResponseBody
	public List<Type> findByVarietyId(Integer varietyId){
		return typeService.findById(varietyId);
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String deleteType(Integer id){
		return typeService.deleteType(id)?"success":"fail";
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public String updateType(Type type){
		return typeService.updateType(type)?"成功":"失败";
	}
	

	/*添加型号*/
	@RequestMapping("/add")
	public String saveType(Type type){
		return typeService.saveType(type)?"redirect:/products/list":"error";
	}
	
	@RequestMapping("/all")
	@ResponseBody
	public LayuiJson<Type> getTypesForVarietyId(Integer id, int page, int limit){
		Page<Object> pages = PageHelper.startPage(page, limit);
		List<Type> list = typeService.findById(id);
		
		LayuiJson<Type> layuiJson = new LayuiJson<>();
		layuiJson.setCount((int) pages.getTotal());
		layuiJson.setData(list);
		
		return layuiJson;
	}
	
	
}
